package com.ice.body.dict.controller;

import com.github.pagehelper.PageHelper;
import com.ice.base.controller.BaseController;
import com.ice.base.result.AjaxResult;
import com.ice.base.result.ResponseResult;
import com.ice.body.dict.model.DictModel;
import com.ice.body.dict.service.DictUnitService;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with GuoYanBin.
 * Description:
 * Date: 2022-05-24
 * Time: 05:19
 */
@RestController
@ApiIgnore(value = "质量单位")
public class DictUnitController extends BaseController{
    //---------------------------质量单位-----------------------start
    @Resource
    private DictUnitService dictUnitService;
    
    @ApiOperation(value = "质量单位-新增")
    @PostMapping("/api/dict/insert")
    public AjaxResult<DictModel> insert_DictUnit(DictModel model) {
        AjaxResult r = ResponseResult.success();
        try{
            model.setCreateTime(new Date());
            dictUnitService.insert(model);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "质量单位-删除")
    @PostMapping("/api/dict/deleteById")
    public AjaxResult<DictModel> deleteById_DictUnit(@RequestParam("id") String id) {
        AjaxResult r = ResponseResult.success();
        try{
            Integer count = dictUnitService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "质量单位-更新")
    @PostMapping("/api/dict/updateById")
    public AjaxResult<DictModel> update_DictUnit(DictModel model) {
        AjaxResult r = ResponseResult.success();
        try{
            dictUnitService.update(model);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "质量单位-查询一个")
    @PostMapping("/api/dict/selectById")
    public AjaxResult<DictModel> selectById_DictUnit(@RequestParam("id") String id) {
        AjaxResult r = ResponseResult.success();
        try{
           DictModel model = dictUnitService.selectById(id);
           r.setData(model);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "质量单位-查询多个")
    @PostMapping("/api/dict/selectAll")
    public AjaxResult<DictModel> selectList_DictUnit(DictModel model) {
        AjaxResult r = ResponseResult.success();
        try{
           List<DictModel> list = dictUnitService.selectListByModel(model);
           r.setData(list);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }

    @ApiOperation(value = "质量单位-查询分页")
    @PostMapping("/api/dict/selectListByPageHelper")
    public AjaxResult<DictModel> selectListByPageHelper_DictUnit(@RequestParam(name = "pageSize") Integer pageSize,
                                                                 @RequestParam(name = "pageNum") Integer pageNum,
                                                                 DictModel model,
                                                                 String keyWord){
        AjaxResult r = ResponseResult.success();
        ArrayList list;
        try{
           PageHelper.startPage(pageNum, pageSize);
           if(StringUtils.isEmpty(keyWord)) {
               list = dictUnitService.selectListByModel(model);
           } else {
               list = dictUnitService.selectListByKeyWord(keyWord);
           }
           getReturnResultByPage(r,list);
        }catch (Exception e){
            e.printStackTrace();
            r = ResponseResult.error();   
        }
        return r;
    }
    //---------------------------质量单位-----------------------end
}
